//
//  HSZJLAreaPickView.m
//  OutSystems
//
//  Created by HiPal on 16/2/23.
//
//

#import "HSZJLAreaPickView.h"

@implementation HSZJLAreaPickView 

-(void)awakeFromNib {
    _provinces = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"citys" ofType:@"plist"]];
    _citys = [_provinces[0] objectForKey:@"citys"];
    _areas = [_citys[0] objectForKey:@"areas"];
    self.pickView.dataSource = self;
    self.pickView.delegate   = self;
}

- (IBAction)cancelClick:(UIButton *)sender {
    [self clearView];
}

- (IBAction)finishClick:(UIButton *)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(hsZJLAreaPickView:)]) {
        [_delegate hsZJLAreaPickView:self];
    }
    [self clearView];
}

#pragma mark -- UIPickerView Delegate
///picker 的列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    //省市县(区)
    return 3;
}
///picker 数据总数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return _provinces.count;
    }else if (component == 1){
        return _citys.count;
    }else{
        return _areas.count;
    }
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component == 0) {
        self.province = [_provinces[row] objectForKey:@"name"];
        return self.province;
    }else if (component == 1){
        self.city = [_citys[row] objectForKey:@"name"];
        return self.city;
    }else{
        self.zipcode =  [_areas[row] objectForKey:@"zipcode"];
        self.area    =  [_areas[row] objectForKey:@"name"];
        return self.area;
    }
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    switch (component) {
        case 0:
        {
            _citys = [_provinces[row] objectForKey:@"citys"];
            [self.pickView selectRow:0 inComponent:1 animated:YES];
            [self.pickView reloadComponent:1];
            
            _areas = [_citys[0] objectForKey:@"areas"];
            [self.pickView selectRow:0 inComponent:2 animated:YES];
            [self.pickView reloadComponent:2];
            
        }
            break;
        case 1:
        {
            _areas = [_citys[row] objectForKey:@"areas"];
            [self.pickView selectRow:0 inComponent:2 animated:YES];
            [self.pickView reloadComponent:2];
            
        }
            break;
        case 2:
        {
            
        }
            break;
        default:
            break;
    }
    
}
/**
 *  消失
 */
- (void)clearView {
    [self removeFromSuperview];
}

@end
